/*
 * Copyright 2020-2030, MateCloud, DAOTIANDI Technology Inc All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Author: pangu(7333791@qq.com)
 */
package com.yc.cloud.portal.controller;

import com.yc.cloud.basic.api.CommonPage;
import com.yc.cloud.basic.api.ResponseResult;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.basic.utils.MyFastJsonUtils;
import com.yc.cloud.entity.MemberAccount;
import com.yc.cloud.oss.service.OssFileService;
import com.yc.cloud.portal.dto.request.MemberAccountUpdateParam;
import com.yc.cloud.portal.dto.request.MemberAccountUpdatePwdParam;
import com.yc.cloud.portal.service.MemberAccountService;
import com.yc.cloud.portal.vo.MemberAccountVo;
import com.yc.cloud.security.utils.MemberUtils;
import com.yc.cloud.wechat.dto.request.WxBarCodeCreateParam;
import io.swagger.annotations.*;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 会员 前端控制器
 * </p>
 *
 * @author Yancy
 * @since 2023-12-23
 */
@RestController
@RequestMapping("/member/accounts")
@Api(value = "会员", tags = "会员接口")
public class MemberAccountController {

    @Resource
    private MemberAccountService memberAccountService;
    @Resource
    private OssFileService ossFileService;


    @GetMapping
    @ApiOperation(value = "会员列表", notes = "支持分页查询,字段模糊匹配,排序等")
    public ResponseResult<CommonPage<MemberAccountVo>> list(BaseQueryParam param) {
        val result = memberAccountService.list(param);
        return ResponseResult.success(CommonPage.restPage(result));
    }


    @GetMapping("{id}")
    @ApiOperation(value = "会员详情", notes = "根据ID查询")
    public ResponseResult<MemberAccountVo> getItem(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val memberAccount = memberAccountService.getById(id);
        val result = memberAccountService.convertVo(memberAccount);
        return ResponseResult.success(result);
    }


    @PostMapping
    @ApiOperation(value = "添加会员", notes = "添加")
    public ResponseResult<MemberAccount> create(@Validated @RequestBody MemberAccount memberAccount) {
        val result = memberAccountService.save(memberAccount);
        if (result) {
            return ResponseResult.success(memberAccount);
        }
        return ResponseResult.failed();
    }


    @ApiOperation(value = "删除会员", notes = "通过ID删除")
    @DeleteMapping(value = "{id}")
    public ResponseResult<Long> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = memberAccountService.removeById(id);
        if (result) {
            return ResponseResult.success();
        }
        return ResponseResult.failed();
    }


    @ApiOperation(value = "修改会员信息", notes = "通过ID修改")
    @PutMapping
    public ResponseResult<Long> update(@Validated @RequestBody MemberAccountUpdateParam param) {
        val result = memberAccountService.update(MemberUtils.getCurrentUseId(), param);
        if (result) {
            return ResponseResult.success();
        }

        return ResponseResult.failed();
    }

    @ApiOperation(value = "修改会员密码", notes = "通过ID修改")
    @PutMapping("updatePwd")
    public ResponseResult<Long> updatePwd(@Validated @RequestBody MemberAccountUpdatePwdParam param) {
        val result = memberAccountService.updatePwd(param);
        return result ? ResponseResult.success() : ResponseResult.failed();
    }

//    @ApiOperation(value = "开通会员VIP", notes = "通过ID修改vip状态值")
//    @PutMapping("vip")
//    public ResponseResult<Long> updateVipFlag() {
//        val memberAccount = memberAccountService.getById(MemberUtils.getCurrentUseId());
//        if (memberAccount != null) {
//            memberAccount.setVip(true);
//            memberAccount.setVipExpireTime(LocalDateTime.now().plusMonths(1));
//        }
//        val updateFlag = memberAccountService.updateById(memberAccount);
//        return updateFlag ? ResponseResult.success() : ResponseResult.failed();
//    }

    @GetMapping("info")
    @ApiOperation(value = "获取当前用户信息", notes = "获取当前用户信息")
    public ResponseResult<MemberAccountVo> getCurrentMemberInfo() {
        val memberAccount = memberAccountService.getById(MemberUtils.getCurrentUseId());
        if (memberAccount.getVipExpireTime() != null && memberAccount.getVipExpireTime().isBefore(LocalDateTime.now())) {
            memberAccount.setVip(false);
            memberAccountService.updateById(memberAccount);
        }
        val result = memberAccountService.convertVo(memberAccount);
        return ResponseResult.success(result);
    }

    @PostMapping("barcode")
    @ApiOperation(value = "生成会员分享二维码", notes = "返回用户二维码访问地址")
    public ResponseResult<String> createQrCode(@RequestBody @Validated WxBarCodeCreateParam param) {
        return ResponseResult.success(memberAccountService.createQrCode(param));
    }
}
